package com.dragee.modules.sys.entity;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.dragee.config.NotNull;
import com.dragee.config.resolver.LongJsonDeserializer;
import com.dragee.config.resolver.LongJsonSerializer;
import com.dragee.modules.work.po.UserCollegePo;
import com.dragee.modules.work.po.UserDeliverPo;
import com.dragee.modules.work.po.UserExpanderPo;
import com.dragee.modules.work.po.UserSellerPo;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * 用户表
 * 
 * @author tys
 * @email tys@mail.com
 * @date 2020-11-26 14:52:45
 */
@Data
@TableName("sys_user")
@NoArgsConstructor
public class UserEntity implements Serializable {


    private static final long serialVersionUID = 1L;

    public UserEntity (UserSellerPo po) {
        this.userId = po.getUserId();
        this.name = po.getName();
        this.username = po.getUsername();
        this.password = po.getPassword();
        this.parentId = po.getParentId();
        this.phone = po.getPhone();
        this.sex = po.getSex();
        this.status = po.getStatus();
        this.createBy = po.getCreateBy();
        this.createTime = po.getCreateTime();
    }

    public UserEntity (UserCollegePo po) {
        this.userId = po.getUserId();
        this.name = po.getName();
        this.username = po.getUsername();
        this.password = po.getPassword();
        this.parentId = po.getParentId();
        this.phone = po.getPhone();
        this.sex = po.getSex();
        this.status = po.getStatus();
        this.createBy = po.getCreateBy();
        this.createTime = po.getCreateTime();
    }

    public UserEntity (UserDeliverPo po) {
        this.userId = po.getUserId();
        this.name = po.getName();
        this.username = po.getUsername();
        this.password = po.getPassword();
        this.parentId = po.getParentId();
        this.phone = po.getPhone();
        this.sex = po.getSex();
        this.status = po.getStatus();
        this.createBy = po.getCreateBy();
        this.createTime = po.getCreateTime();
    }

    public UserEntity (UserExpanderPo po) {
        this.userId = po.getUserId();
        this.name = po.getName();
        this.username = po.getUsername();
        this.password = po.getPassword();
        this.parentId = po.getParentId();
        this.phone = po.getPhone();
        this.sex = po.getSex();
        this.status = po.getStatus();
        this.createBy = po.getCreateBy();
        this.createTime = po.getCreateTime();
    }

    /**
     * userId
     */
    @TableId
    @JsonSerialize(using = LongJsonSerializer.class)
    @JsonDeserialize(using = LongJsonDeserializer.class)
    private Long userId;

    /**
     * 名字
     */
    @NotNull("姓名")
    private String name;

    private Long parentId;

    /**
     * 用户名
     */
    @NotNull("账号")
    private String username;

    /**
     * 密码
     */
    private String password;

    /**
     * 头像
     */
    private String avatar;
    /**
     * 电话
     */
    private String phone;

    /**
     * 性别
     */
    private String sex;

    /**
     * 邮箱
     */
    private String email;


    /**
     * 用户状态
     */
    private String status;

    private String points;


    /**
     * 创建人
     */
    @TableField(fill = FieldFill.INSERT)
    private String createBy;

    /**
     * 创建时间
     */
    @TableField(fill = FieldFill.INSERT)
    private Date createTime;

    /**
     * 修改人
     */
    @TableField(fill = FieldFill.UPDATE)
    private String updateBy;

    /**
     * 修改时间
     */
    @TableField(fill = FieldFill.UPDATE)
    private Date updateTime;

    /**
     * 用户角色
     */
    @TableField(exist = false)
    private String roleIds;

    @TableField(exist = false)
    private String roleNames;

    @TableField(exist = false)
    private List<UserEntity> chirdrens;

    public boolean isAdmin()
    {
        return isAdmin(this.userId);
    }

    public static boolean isAdmin(Long userId)
    {
        return userId != null && 1L == userId;
    }

}